#include <iostream>
#include <string>
#include <memory>
#include <sstream>
#include <jsoncpp/json/json.h>

void Writer()
{
  Json::Value root;
  const char* name="小明";
  int age=18;
  float grade[]={78.5,98.5,67.5};
  root["姓名"]=name;
  root["年龄"]=age;
  root["成绩"].append(grade[0]);
  root["成绩"].append(grade[1]);
  root["成绩"].append(grade[2]);

  Json::StreamWriterBuilder swb;
  std::unique_ptr<Json::StreamWriter> sw(swb.newStreamWriter());
  std::stringstream ss; 
  sw->write(root,&ss);
  std::cout<<ss.str()<<std::endl;
}

void Reader()
{
  std::string str=R"({"姓名":"小明","年龄":18,"成绩":[78.5,98.5,67.5]})";
  Json::Value root;
  Json::CharReaderBuilder crb;
  std::unique_ptr<Json::CharReader> cr(crb.newCharReader());
  std::string err;
  cr->parse(str.c_str(),str.c_str()+str.size(),&root,&err);
  std::cout<<root["姓名"].asString()<<std::endl;
  std::cout<<root["年龄"].asInt()<<std::endl;
  for(auto it=root["成绩"].begin();it!=root["成绩"].end();it++)
  {
    std::cout<<it->asFloat()<<std::endl;
  }
}

int main()
{
  //Writer();
  Reader();
  return 0;
}
